package com.zweicom.bconsumo.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.zweicom.bconsumo.model.Perfil;

@Repository
@Transactional
public class PerfilDAO {
	
	//@Autowired
	private SessionFactory sessionFactory;
	
	/**
	  * Retrieves a single perfil by id
	  */
	public Perfil get(int id) {
		return (Perfil) sessionFactory.getCurrentSession().get(Perfil.class, id);
	}
	
	/**
	  * Retrieves all perfiles
	  *
	  * @return a list of perfiles
	  */
	@SuppressWarnings("unchecked")
	public List<Perfil> getAll() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Perfil.class);
		return criteria.list();
	}
	
	/**
	  * Search on perfiles
	  *
	  * @return a list of perfiles
	  */
	@SuppressWarnings("unchecked")
	public List<Perfil> search(String name) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Perfil.class);
		criteria.add(Restrictions.ilike("nombre", name+"%"));
		return criteria.list();
	}
	
	/**
	  * Adds a new perfil
	  */
	public int add(Perfil perfil) {
		return (Integer) sessionFactory.getCurrentSession().save(perfil);
	}
	
	/**
	  * Deletes an existing perfil
	  * @param id the id of the existing perfil
	  */
	public void delete(int id) {
		Perfil perfil = get(id);
		sessionFactory.getCurrentSession().delete(perfil);
	}
	
	/**
	  * Edits an existing perfil
	  */
	public void edit(Perfil perfil) {
		sessionFactory.getCurrentSession().merge(perfil);
	}
	
}